#include <iostream>
#include <set>
#include <cmath>
using namespace std;
template <typename Container>
void display(const Container &con)
{
    for (auto elem : con)
    {
        cout << elem << " ";
    }
    cout << endl;
}
class Point
{
public:
    Point(int x, int y)
        : _x(x), _y(y) {}
    double getinstance() const
    {
        return hypot(_x, _y);
    }
    friend ostream &operator<<(std::ostream &os, const Point &rhs);
    friend bool operator<(const Point &lhs, const Point &rhs);
    friend struct std::less<Point>;
    friend struct CompareList;

private:
    int _x;
    int _y;
};
std::ostream &operator<<(std::ostream &os, const Point &rhs)
{
    os << "(" << rhs._x
       << "," << rhs._y << ")"
       << endl;
    return os;
}
// 1.重载运算符
bool operator<(const Point &lhs, const Point &rhs)
{
    cout <<"operator<" << endl;
    if (lhs.getinstance() < rhs.getinstance())
    {
        return true;
    }
    else if (lhs.getinstance() == rhs.getinstance())
    {
        if (lhs._x < rhs._x)
        {
            return true;
        }
        else if (lhs._x == rhs._x)
        {
            if (lhs._y < rhs._y)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;
    }
}
// 模板特化
template <>
struct std::less<Point>
{
    bool operator()(const Point &lhs, const Point &rhs) const
    {
        cout << "std::less<Point>" << endl;
        if (lhs.getinstance() < rhs.getinstance())
        {
            return true;
        }
        else if (lhs.getinstance() == rhs.getinstance())
        {
            if (lhs._x < rhs._x)
            {
                return true;
            }
            else if (lhs._x == rhs._x)
            {
                if (lhs._y < rhs._y)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
};
//3.函数参数需要在set中主动调用
struct CompareList{
    bool operator()(const Point &lhs, const Point &rhs) const
    {
        cout << "CompareList" << endl;
        if (lhs.getinstance() < rhs.getinstance())
        {
            return true;
        }
        else if (lhs.getinstance() == rhs.getinstance())
        {
            if (lhs._x < rhs._x)
            {
                return true;
            }
            else if (lhs._x == rhs._x)
            {
                if (lhs._y < rhs._y)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
};
void test()
{
    set<Point,CompareList> number = {
        Point(1, 2),
        Point(3, 2),
        Point(-1, 2),
        Point(-2, 2),
        Point(1, 2),
        Point(1, -2),
    };
    display(number);
}
int main()
{
    test();
    return 0;
}
